package com.ctrlspace.config;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;

public class ConfigurationManager {
	
	private static final String CONFIG_FILE = "A:\\CtrlSpace\\config.json";
	
	private static ConfigurationManager _instance; 
	
	private JSONObject configuration;
	
	private ConfigurationManager() {
		loadSettings();
	}
	
	private void loadSettings() {
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(CONFIG_FILE));
			
			configuration = (JSONObject) JSONValue.parseWithException(br);
		} catch (FileNotFoundException e) {
			// TODO Create custom exception
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Create custom exception
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Create custom exception
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					// TODO Create custom exception
					e.printStackTrace();
				}
			}
		}
	}

	public Map<?, ?> getConfiguration() {
		return configuration;
	}
	
	public static ConfigurationManager getInstance() {
		if (_instance == null) {
			_instance = new ConfigurationManager();
		}
		
		return _instance;
	}
	
	public static Object get(Object key) {
		return getInstance().getConfiguration().get(key);
	}
	
	public static void reload() {
		getInstance().loadSettings();
	}
}
